import cv2
import numpy as np

def access_pixels(frame):
  print(frame.shape)  #shape内包含三个元素：按顺序为高、宽、通道数
  height = frame.shape[0]
  weight = frame.shape[1]
  channels = frame.shape[2]
  print("weight : %s, height : %s, channel : %s" %(weight, height, channels))
  
  for row in range(height):            #遍历高
    for col in range(weight):         #遍历宽
        for c in range(channels):     #便利通道
          pv = frame[row, col, c]
          frame[row, col, c] = 255 - pv     #全部像素取反，实现一个反向效果
  cv2.imshow("fanxiang", frame)
    
image_file = "..\\temp\\1b4dd83f7b614b7d90bb8b4d021c237e-xyz-2.png"
# image_file = "..\\temp\\1b4dd83f7b614b7d90bb8b4d021c237e-tunnel-10.png"
image = cv2.imread(image_file)
cv2.imshow("origin image", image)
# access_pixels(image)
cv2.waitKey(0)

# 截取
crop_image = image[0:20, 0:12]
pixel_data = np.array(crop_image, dtype = np.uint8)
# print(pixel_data)
print(crop_image.shape, '截图的像素和:',np.sum(pixel_data<255))
cv2.imshow("crop_image", crop_image)
cv2.waitKey(0)